package ua.com.infopulse.activities;

import ua.com.infopulse.R;
import ua.com.infopulse.TransferKey;
import ua.com.infopulse.db.DBGetter;
import ua.com.infopulse.db.DataTrackManager;
import ua.com.infopulse.adapters.PointListAdapter;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;


/**
 * @author Andrew.Nazymko
 */
public class PointList extends android.app.ListActivity {
    private ListView listView;
    private String TAG = PointList.class.toString();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_in_list);
        PointListAdapter mainListAdapter = getListAdapter();
        initListView(mainListAdapter);

    }

    public PointListAdapter getListAdapter() {
        DataTrackManager manager = DBGetter.getDTM();
        return new PointListAdapter(manager.getCheckInList(), getApplication());
    }


    private void initListView(PointListAdapter mainList) {
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Integer tag = (Integer) view.getTag();
                Intent startIntent = new Intent(getApplication(), PointInfo.class);
                startIntent.putExtra(TransferKey.ID, tag);
                startActivity(startIntent);

            }
        });
        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                AlertDialog.Builder deleteDialog = new AlertDialog.Builder(getListView().getContext());
                deleteDialog.setTitle(getString(R.string.delete_question));
                String text = ((TextView) view.findViewById(R.id.check_in_text_message)).getText().toString();
                final int tag_id = Integer.parseInt(view.getTag().toString());
                deleteDialog.setMessage(getString(R.string.confirm_delete_message) + text + "\"?");
                deleteDialog.setPositiveButton(getString(R.string.delete), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int int_value) {
                        DataTrackManager manager = DBGetter.getDTM();
                        manager.deletePoint(tag_id);
                        getListAdapter().refresh();
                    }
                });

                deleteDialog.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                      //                        dialogInterface.dismiss();

                    }
                });

                deleteDialog.show();
                return true;


            }
        });
        setListAdapter(mainList);
    }
}
